import 'dart:io';

import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:get_it/get_it.dart';
import 'package:responsive_framework/breakpoint.dart';
import 'package:responsive_framework/responsive_breakpoints.dart';
import 'config/route/bt_app_router.dart';
import 'data/db/bt_database_helper.dart';
import 'data/store/bt_google_translation_store.dart';
import 'data/store/bt_hive_logon_store.dart';
import 'package:bot_toast/bot_toast.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  BtHiveLogonStore().initHive();
  DatabaseHelper().initDatabase();
  BtGoogleTranslationStore().init();

  GetIt.instance.registerSingleton<BtAppRouter>(BtAppRouter());
  final savedThemeMode = await AdaptiveTheme.getThemeMode();
  await SystemChrome.setPreferredOrientations(
    [
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ],
  );

  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }

  runApp(BrewTunesApp(
    savedThemeMode: savedThemeMode,
  ));
}

class BrewTunesApp extends StatelessWidget {
  BrewTunesApp({super.key, this.savedThemeMode});

  final AdaptiveThemeMode? savedThemeMode;
  final _appRouter = GetIt.instance<BtAppRouter>();
  final botToastBuilder = BotToastInit(); //1. call BotToastInit

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return AdaptiveTheme(
      light: ThemeData(
          useMaterial3: true,
          // 亮色主题的背景色
          scaffoldBackgroundColor: const Color(0xFFF5F6F7),
          colorScheme: ColorScheme.fromSeed(
              seedColor: Colors.blue, primary: Colors.blue, brightness: Brightness.light),
          floatingActionButtonTheme: const FloatingActionButtonThemeData(elevation: 3),
          appBarTheme: const AppBarTheme(
              centerTitle: true,
              titleTextStyle: TextStyle(fontSize: 14, color: Colors.black),
              backgroundColor: Colors.white,
              systemOverlayStyle: SystemUiOverlayStyle(statusBarColor: Colors.transparent),
              iconTheme: IconThemeData(color: Colors.black))),
      //dark: ThemeData.dark(useMaterial3: true),
      initial: savedThemeMode ?? AdaptiveThemeMode.light,
      builder: (theme, darkTheme) => MaterialApp.router(
        title: '',
        debugShowCheckedModeBanner: false,
        theme: theme,
        darkTheme: darkTheme,
        routerConfig: _appRouter.config(navigatorObservers: () => [BotToastNavigatorObserver()]),
        // routerDelegate: AutoRouterDelegate(_appRouter),
        // routeInformationProvider: _appRouter.routeInfoProvider(),
        // routeInformationParser: _appRouter.defaultRouteParser(),
        builder: EasyLoading.init(builder: (context, child) {
          var builder = ResponsiveBreakpoints.builder(
            child: child!,
            breakpoints: [
              const Breakpoint(start: 0, end: 450, name: MOBILE),
              const Breakpoint(start: 451, end: 800, name: TABLET),
              const Breakpoint(start: 801, end: 1920, name: DESKTOP),
              const Breakpoint(start: 1921, end: double.infinity, name: '4K'),
            ],
          );
          child = botToastBuilder(context, builder);
          return Scaffold(
            body: GestureDetector(
              onTap: () {
                FocusScopeNode currentFocus = FocusScope.of(context);
                if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
                  FocusManager.instance.primaryFocus?.unfocus();
                }
              },
              child: Center(
                child: child,
              ),
            ),
          );
        }),
        localizationsDelegates: const [
          GlobalMaterialLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          DefaultMaterialLocalizations.delegate,
          DefaultWidgetsLocalizations.delegate,
        ],
        supportedLocales: const [
          Locale('en', ''),
          Locale('zh', 'CN'),
        ],
      ),
    );
  }
}
